//
//  HotelViewController.m
//  Conference
//
//  Created by Xiang He on 12-11-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "HotelViewController.h"
#import "AppDelegate.h"
#import "Member.h"
#import "Globals.h"
#import "Constant.h"
#import "WebData.h"
#import "HotelData.h"
#import "DateTool.h"
#import "NetContant.h"
@interface HotelViewController ()

@end

@implementation HotelViewController
@synthesize bottomView;
@synthesize img_addressAndPhone_bg;
@synthesize img_shareInfo_bg;
@synthesize img_baseInfo_bg;
@synthesize lab_shareInfo;
@synthesize lab_baseInfo;
@synthesize lab_phone;
@synthesize lab_address;
@synthesize hotelData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.img_baseInfo_bg.image=[[UIImage imageNamed:@"msg_bg_normal.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    self.img_shareInfo_bg.image=[[UIImage imageNamed:@"msg_bg_normal.png"]  stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    self.img_addressAndPhone_bg.image=[[UIImage imageNamed:@"msg_bg_normal.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    [self loadingData];
    // Do any additional setup after loading the view from its nib.
}
-(void)loadingData{
    Member *member= [(AppDelegate *)[[UIApplication sharedApplication] delegate]userMember];
    self.hotelData=member.hotelData;
//    if (self.hotelData==nil) {
        NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
        [thread start];
//    }else
//        [self showData];
    
}
-(void)showData{
    
    
//    if (self.hotelData==nil) {
//        return;
//    }
    
    NSString *name=self.hotelData.hotelName;
    NSString *type=self.hotelData.bookHotelFlag;
    NSString *checkTime=self.hotelData.checkTime;
    NSString *moveTime=self.hotelData.moveTime;
    NSString *payStatus=self.hotelData.payStatus;
    NSString *payMoney=self.hotelData.payMoney;
    NSString *hasPaid=self.hotelData.hasPaid;
    NSString *notPay=self.hotelData.notPay;
    NSMutableString *sbInfo=[NSMutableString stringWithCapacity:0];
    
    [sbInfo appendFormat:@"酒店名称：%@\r\n",name];
    [sbInfo appendFormat:@"预定房型：%@\r\n",type];
    
    if(checkTime!=nil && ![checkTime isEqualToString:@""]){
        [sbInfo appendFormat:@"入住时间：%@\r\n",    [[DateTool getInstance] getTimeByDurationForString:checkTime]];
    }else {
        [sbInfo appendFormat:@"入住时间：\r\n"];
    }
    
    if(moveTime!=nil && ![moveTime isEqualToString:@""]){
        [sbInfo appendFormat:@"退房时间：%@\r\n",    [[DateTool getInstance] getTimeByDurationForString:moveTime]];
    }else {
        [sbInfo appendFormat:@"退房时间：\r\n"];
    }
    if(payStatus!=nil && ![payStatus isEqualToString:@""]){
        [sbInfo appendFormat:@"支付情况：%@\r\n",payStatus];
    }else {
        [sbInfo appendFormat:@"支付情况：\r\n"];
    }
    if(payMoney!=nil && ![payMoney isEqualToString:@""]){
        [sbInfo appendFormat:@"总金额　：%@\r\n",payMoney];
    }else {
         [sbInfo appendFormat:@"总金额　：\r\n"];
    }
    if(hasPaid!=nil && ![hasPaid isEqualToString:@""]){
        [sbInfo appendFormat:@"已       付：%@\r\n",hasPaid];
    }else {
        [sbInfo appendFormat:@"已       付：\r\n"];
    }
    if(notPay!=nil && ![notPay isEqualToString:@""]){
        [sbInfo appendFormat:@"未       付：%@\r\n",notPay];
    }else {
        [sbInfo appendFormat:@"未       付：\r\n"];
    }
    self.lab_baseInfo.text=sbInfo;
    
    
    if(self.hotelData.shareCustName!=nil && ![@"" isEqualToString:self.hotelData.shareCustName]){
        
        NSString *shareInfo=[NSString stringWithFormat:@"同住人姓名:%@\r\n同住人公司:%@",self.hotelData.shareCustName,self.hotelData.shareCustCompany];
        self.lab_shareInfo.text=shareInfo;
        [self.img_shareInfo_bg setHidden:NO];
    }else {
        [self.img_shareInfo_bg setHidden:YES];
        self.bottomView.frame=CGRectMake(10, 190, 300, 60);
    }
    
    self.lab_phone.text=[NSString stringWithFormat:@"电话：%@",self.hotelData.phone];
    self.lab_address.text=[NSString stringWithFormat:@"地址：%@", self.hotelData.hotleAddress];
    
}
-(void)run{
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=[[WebData getInstance]getHotelInfo];
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
        // [[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
        // [[Globals getInstance] showAlertView:[[Constant getInstance] data_isempty]];
        
    }else {
        @try {
            [self doNetRet:sRet];
            [self doNetInfo];
        }
        @catch (NSException *exception) {
            
        }
        
        
    }
    
}

-(void)doNetRet:(NSString *)sRet{
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
    if(nil!=[tables objectAtIndex:0]){
        NSArray *record=[[tables objectAtIndex:0]componentsSeparatedByString:[NetContant getInstance].RECORDS_SPLIT_FLAG];
        if([record count]>=11){
            HotelData *hoteldata=[[HotelData alloc]init];
            hoteldata.hotelName=[record objectAtIndex:0];
            hoteldata.phone = [record objectAtIndex:1];
            hoteldata.hotleAddress = [record objectAtIndex:2];
            hoteldata.cordX=[record objectAtIndex:3];
            hoteldata.cordY=[record objectAtIndex:4];
            
            hoteldata.payMoney=[record objectAtIndex:5];
            hoteldata.payStatus=[record objectAtIndex:6];
            hoteldata.hasPaid=[record objectAtIndex:7];
            hoteldata.notPay=[record objectAtIndex:8];
            
            hoteldata.roomType=[record objectAtIndex:9];
            hoteldata.checkTime=[record objectAtIndex:10];
            hoteldata.moveTime=[record objectAtIndex:11];
            hoteldata.bookHotelFlag=[record objectAtIndex:12];
            hoteldata.shareCustCompany=[record objectAtIndex:13];
            hoteldata.shareCustName=[record objectAtIndex:14];
            Member *member=[(AppDelegate *)[[UIApplication sharedApplication] delegate]userMember];
            member.hotelData=hoteldata;
            self.hotelData=hoteldata;
            [hoteldata release];
            
        }
    }
}
-(void)doNetInfo{
    [self showData];
    
}
- (void)viewDidUnload
{
    [self setHotelData:nil];
    [self setLab_phone:nil];
    [self setLab_address:nil];
    [self setLab_baseInfo:nil];
    [self setLab_shareInfo:nil];
    [self setImg_baseInfo_bg:nil];
    [self setImg_shareInfo_bg:nil];
    [self setImg_addressAndPhone_bg:nil];
    [self setBottomView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [hotelData release];
    [lab_phone release];
    [lab_address release];
    [lab_baseInfo release];
    [lab_shareInfo release];
    [img_baseInfo_bg release];
    [img_shareInfo_bg release];
    [img_addressAndPhone_bg release];
    [bottomView release];
    [super dealloc];
}
@end
